
<!DOCTYPE html>
<html>
<head>[#include "../manage/common/head.html" /]
</head>

<body class="gray-bg">
	<div class="wrapper wrapper-content">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>发布宝贝</h5>
					</div>
					<div class="ibox-content">
						<button id="add" type="button" class="btn btn-primary pull-right"
							data-toggle="modal" data-title="新建" data-target="#windowModal">添加</button>
						<table
							class="table table-striped table-bordered table-hover dataTables">
							<thead>
								<tr>
									<th class="text-center">商品名称</th>
									<th class="text-center">商品简介</th>
									<th class="text-center">商品类别</th>
									<!-- <th class="text-center">是否为精品</th>
									<th class="text-center">是否热销</th>
									<th class="text-center">是否是新品</th>
									<th class="text-center">是否免运费</th> -->
									<th class="text-center">销量</th>
									<th class="text-center">好评数</th>
									<th class="text-center">中等评价数</th>
									<th class="text-center">差评数</th>
									<th class="text-center">操作项</th>
								</tr>
							</thead>
							<tbody>
							</tbody>
						</table>

					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="modal fade" id="windowModal" tabindex="-1" role="dialog"
		aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title">新建</h4>
				</div>

				<div class="modal-body">
					<form class="form-horizontal" id="myForm">
						<div class="form-group">
							<label class="col-sm-2 control-label">商品名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="name">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">商品简介</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="title">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">商品类别</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="categoryName"
									onclick="treeViewShowHide()">
							</div>
							<div class="col-sm-2"></div>
							<div class="col-sm-10">
								<div id="treeview" class="test"
									style="display: none; height: 100%;"></div>
								<div id="categoryID" class="hidden"></div>
							</div>
						</div>
						<!-- <div class="form-group">
							<label class="col-sm-2 control-label">是否为精品</label>
							<div class="col-sm-10">
								<select id="isBest" class="selectpicker show-tick form-control"
									data-live-search="false">
									<option value=""></option>
									<option value="0">否</option>
									<option value="1">是</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否热销</label>
							<div class="col-sm-10">
								<select id="isHot" class="selectpicker show-tick form-control"
									data-live-search="false">
									<option value=""></option>
									<option value="0">否</option>
									<option value="1">是</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否是新品</label>
							<div class="col-sm-10">
								<select id="isNew" class="selectpicker show-tick form-control"
									data-live-search="false">
									<option value=""></option>
									<option value="0">否</option>
									<option value="1">是</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否免运费</label>
							<div class="col-sm-10">
								<select id="isFree" class="selectpicker show-tick form-control"
									data-live-search="false">
									<option value=""></option>
									<option value="0">否</option>
									<option value="1">是</option>
								</select>
							</div>
						</div> -->
						<div class="form-group">
							<label class="col-sm-2 control-label">销量</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" min="0" id="sell" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">好评数</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" min="0" id="bestCount" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">中等评价数</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" min="0"
									id="mediumCount" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">差评数</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" min="0" id="badCount" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">图文详情</label>
							<div class="col-sm-10">
								<div id="summernote"></div>
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer clearfix">
					<div class="form-group col-md-8 col-sm-offset-2 text-center">
						<button type="button" class="btn btn-primary" id="save">保存</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	[#include "../manage/common/js.html" /]
	<script>
	$(function() {
		dt = $('.dataTables').dataTable(
			{
				"ordering" : false,
				"searching" : false,
				"info" : true,
				"bLengthChange" : false,
				"pagingType" : "full_numbers",
				"language" : {
					"lengthMenu" : "每页 _MENU_ 条记录",
					"zeroRecords" : "没有找到记录",
					"info" : "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )",
					"infoEmpty" : "无记录",
					"infoFiltered" : "(从 _MAX_ 条记录过滤)",
					"sEmptyTable" : "表中数据为空",
					"sLoadingRecords" : "载入中...",
					"oPaginate" : {
						"sFirst" : "首页",
						"sPrevious" : "上一页",
						"sNext" : "下一页",
						"sLast" : "末页"
					}
				},
				"serverSide" : true,
				"sAjaxSource" : "${base}/manage/goods/selectInfoList",
				"columns" : [
						{
							"data" : "name",
							"sWidth" : "10%"
						},
						{
							"data" : "title",
							"sWidth" : "20%"
						},
						{
							"data" : "categoryName",
							"sWidth" : "10%"
						}
						/* ,
						{
							"data" : "isBest",
							"sWidth" : "5%",
							"render" : function(data) {
								if (data == 0) {
									return '否'
								} else if (data == 1) {
									return '是'
								} else
									return "";
							}
						},
						{
							"data" : "isHot",
							"sWidth" : "5%",
							"render" : function(data) {
								if (data == 0) {
									return '否'
								} else if (data == 1) {
									return '是'
								} else
									return "";
							}
						},
						{
							"data" : "isNew",
							"sWidth" : "5%",
							"render" : function(data) {
								if (data == 0) {
									return '否'
								} else if (data == 1) {
									return '是'
								} else
									return "";
							}
						},
						{
							"data" : "isFree",
							"sWidth" : "5%",
							"render" : function(data) {
								if (data == 0) {
									return '否'
								} else if (data == 1) {
									return '是'
								} else
									return "";
							}
						} */
						,
						{
							"data" : "sell",
							"sWidth" : "10%"
						},
						{
							"data" : "bestCount",
							"sWidth" : "10%"
						},
						{
							"data" : "mediumCount",
							"sWidth" : "10%"
						},
						{
							"data" : "badCount",
							"sWidth" : "10%"
						},
						{
							"data" : {
								"id" : "id"
							},
							"sWidth" : "10%",
							"render" : function(data) {
								return '<div class="text-center"><button class="btn btn-info btn-sm" type="button" data-title="编辑" data-toggle="modal" data-target="#windowModal" data-id="'
										+ data.id
										+ '" onclick="edit('
										+ data.id
										+ ')">编辑</button>'
										+ '&nbsp;&nbsp;<button class="btn btn-warning btn-sm" type="button" onclick="del('
										+ data.id
										+ ')">删除</button><div>';
							}
						} ],
				"fnServerData" : function(sSource, aoData,
						fnCallback) {
					$.ajax({
						"dataType" : 'json',
						"type" : "POST",
						"url" : sSource,
						"data" : aoData,
						"success" : fnCallback
					});
				}
			});

			$('#summernote').summernote(
			{
				height : 150,
				lang : 'zh-CN',
				fontNames : [ 'Microsoft YaHei', 'DengXian', '新宋体',
						'黑体' ],
				callbacks : {
					onImageUpload : function(files) {
						var formData = new FormData();
						formData.append('file', files[0]);
						formData.append('imgType', 4);
						$.ajax({
							url : '${base}/manage/upload',//后台文件上传接口
							type : 'POST',
							data : formData,
							dataType : 'json',
							processData : false,
							contentType : false,
							success : function(data) {
								$('#summernote').summernote(
										'insertImage',
										'${base}/' + data.file.path,
										'img');
							}
						});
					}
				}
			});
		})

		function search() {
			dt.fnFilter();
		}

		function restForm() {
			$("#name").val("");
			$("#title").val("");
			/* $("#isBest").val("");
			$("#isHot").val("");
			$("#isNew").val("");
			$("#isFree").val(""); */
			$("#sell").val(0);
			$("#bestCount").val(0);
			$("#mediumCount").val(0);
			$("#badCount").val(0);
			$('#img1').empty();
			$('#progress').empty();
			$("#treeview").hide();
			$("#categoryID").val("");
			$("#categoryName").val("");
			$('#summernote').summernote('code', "");
		}

		var oid = 0;

		$('#windowModal').on(
				'show.bs.modal',
				function(event) {
					var button = $(event.relatedTarget);
					var recipient = button.data('title');
					var modal = $(this);
					modal.find('.modal-title').text(recipient);
					if (recipient == "编辑") {
						$.post("${base}/manage/goods/getInfo/"
								+ button.data('id'), function(r) {
							oid = r.infoDate.id;
						}, 'json')
					} else {
						oid = 0;
					}
				});

		$("#windowModal").on("hidden.bs.modal", function() {
			restForm();
		});

		function treeViewShowHide() {
			$.post("${base}/manage/goods/selectCategoryList", function(r) {
				$("#treeview").treeview({
					levels : 1,
					data : r,
					onNodeSelected : function(e, o) {
						$("#categoryID").val("");
						$("#categoryName").val("");
						$("#categoryID").val(o.id);
						$("#categoryName").val(o.text);
						$("#treeview").hide();
					},
					backColor : "#DDDDDD"
				})
				$("#treeview").show();
			}, 'json')
		}

		$("#save").click(function() 
				{
					var name = $("#name").val().trim();
					if (name == "") {
						swal("警告", "请填写商品名称！", "warning");
						return;
					}
					var title = $("#title").val().trim();
					if (title == "") {
						swal("警告", "请填写商品标题！", "warning");
						return;
					}
					var categoryID = $("#categoryID").val();
					if (categoryID == "") {
						swal("警告", "请选择商品类别！", "warning");
						return;
					}
					var categoryName = $("#categoryName").val().trim();
					if (categoryName == "") {
						swal("警告", "请选择商品类别！", "warning");
						return;
					}
					/* var isBest = $("#isBest").val();
					if (isBest == "") {
						swal("警告", "请选择是否为精品！", "warning");
						return;
					}
					var isHot = $("#isHot").val();
					if (isHot == "") {
						swal("警告", "请选择是否热销！", "warning");
						return;
					}
					var isNew = $("#isNew").val();
					if (isNew == "") {
						swal("警告", "请选择是否是新品！", "warning");
						return;
					}
					var isFree = $("#isFree").val();
					if (isFree == "") {
						swal("警告", "请选择是否免运费！", "warning");
						return;
					} */
					var sell = $("#sell").val();
					if (sell == "") {
						sell = 0;
					}
					var bestCount = $("#bestCount").val();
					if (bestCount == "") {
						bestCount = 0;
					}
					var mediumCount = $("#mediumCount").val();
					if (mediumCount == "") {
						mediumCount = 0;
					}
					var badCount = $("#badCount").val();
					if (badCount == "") {
						badCount = 0;
					}
					var synopsis = $('#summernote').summernote('code');
					$.post("${base}/manage/goods/saveInfo", {
						id : oid,
						name : name,
						title : title,
						/* isBest : isBest,
						isHot : isHot,
						isNew : isNew,
						isFree : isFree, */
						sell : sell,
						bestCount : bestCount,
						mediumCount : mediumCount,
						badCount : badCount,
						categoryID : categoryID,
						synopsis : synopsis
					}, function(r) {
						if (r.code == 0) {
							swal("成功", "保存完成！", "success");
							$('#windowModal').modal('hide');
							search();
						} else {
							swal("错误", r.msg, "error");
						}
					}, 'json')
				})

		function edit(id) {
			$.post("${base}/manage/goods/getInfo/"+id,function(r){
				
				$("#name").val(r.infoDate.name);
				$("#title").val(r.infoDate.title);
				/* $("#isBest").val(r.infoDate.isBest);*/
				/*$("#isHot").val(r.infoDate.isHot);
				$("#isNew").val(r.infoDate.isNew);
				$("#isFree").val(r.infoDate.isFree); */
				$("#sell").val(r.infoDate.sell);
				$("#bestCount").val(r.infoDate.bestCount);
				$("#mediumCount").val(r.infoDate.mediumCount);
				$("#badCount").val(r.infoDate.badCount);
				$("#categoryID").val(r.infoDate.categoryID);
				$("#categoryName").val(r.infoDate.categoryName);
				$('#summernote').summernote('code', r.infoDate.synopsis);
				oid = id;
			},'json')
		}

		function del(id) {
			swal({
				title : '确定要删除吗?',
				type : 'warning',
				showCancelButton : true,
				cancelButtonColor : '#3085d6',
				confirmButtonColor : '#d33',
				confirmButtonText : '是的,确定!',
				cancelButtonText : '取消'
			}).then(function() {
				$.post("${base}/manage/goods/delInfo/" + id, function(r) {
					if (r.code == 0) {
						swal("成功", "已经删除！", "success");
						search();
					}
				}, 'json')
			})
		}
	</script>
</body>
</html>
